// pages/my/orbit/orbit.js
const app = getApp();
const db = wx.cloud.database();
const myOrbit = db.collection("myOrbit");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAll:false,
    page:0,
    orbit:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.globalData.getOrbit=true;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (typeof that.getTabBar === 'function' &&
      that.getTabBar()) {
      that.getTabBar().setData({
        pageCur: 'myOrbit',
      })
    }
    if (app.globalData.getOrbit) {
      that.setData({
        isAll: false,
        page: 0,
        orbit: [],
      });
      wx.cloud.callFunction({
        name: 'getMyOrbit',
        data: {
          page: that.data["page"],
        },
        success: function (res) {
          that.setData({
            orbit: that.data["orbit"].concat(res.result.data)
          });
          if (res.result.data.length == 100) {
            that.setData({
              page: that.data["page"] + 100
            });
          } else {
            that.setData({
            isAll: true
            });
            app.globalData.getOrbit = false;
          }
        },
        fail: err => {
          console.log(err);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data["isAll"] == false) {
      wx.cloud.callFunction({
        name: 'getMyOrbit',
        data: {
          page: that.data["page"],
        },
        success: function (res) {
          that.setData({
            orbit: that.data["orbit"].concat(res.result.data)
          });
          if (res.result.data.length == 100) {
            that.setData({
              page: that.data["page"] + 100
            });
          } else {
            that.setData({
              isAll: true
            });
            app.globalData.getOrbit= false;
          }

        },
        fail: err => {
          console.log(err);
        }
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title:"疫轨迹，了解感染者出行活动，发现自身感染风险",
      imageUrl:"/images/share.png",
      path:"/pages/sufferer/travel/travel",
    }
  },



  delMyOrbit: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.orbitid;
    wx.showModal({
      title: '确认删除这条数据 ？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          myOrbit.doc(id).remove({
            success: function (res) {
              wx.showToast({
                title: '删除成功',
                icon: 'none',
                duration: 1500
              });
              that.getMyOrbit();
            },
          });
        }
      }
    });
  },


  getMyOrbit: function () {
    var that = this;
    that.setData({
      isAll: false,
      page: 0,
      orbit: [],
    });
    wx.cloud.callFunction({
      name: 'getMyOrbit',
      data: {
        page: that.data["page"],
      },
      success: function (res) {
        that.setData({
          orbit: that.data["orbit"].concat(res.result.data)
        });
        if (res.result.data.length == 100) {
          that.setData({
            page: that.data["page"] + 100
          });
        } else {
          that.setData({
            isAll: true
          });
          app.globalData.getOrbit = false;
        }
      },
      fail: err => {
        console.log(err);
      }
    });
  },

  showOrbit:function(e){
    var id = e.currentTarget.dataset.orbitid;
    wx.navigateTo({
      url: '/pages/showOrbit/showOrbit?id='+id,
    })
  }


})